package com.yang.thread.syn;

import sun.security.provider.certpath.Vertex;

import java.util.*;

/**
 * @author yds
 * @program thread-study
 * @description 测试ArraryList线程不安全问题
 * @create 2020-09-23 15:52
 **/
public class ThreadNoSafeList {
    private static Object a=new Object();
    public static void main(String[] args) {
        List<String> list=new ArrayList<>();
        List<String> list1= Collections.synchronizedList(new ArrayList<>());//线程安全 通过代码发现并未添加到1万个元素
        Vector<String> list2=new  Vector<String>();
        for (int i = 0; i < 10000; i++) {

            new Thread(() -> {
                //list.add(Thread.currentThread().getName());
                synchronized (a){
                list1.add(Thread.currentThread().getName());}
            }).start();

        }
        System.out.println(list.size());
        System.out.println(list1.size());

    }
}